const childProcess = require('child_process'); // 继承了 EventEmitter
const { resolve } = require('path');

/**
 * 派生子进程
 */
(async () => {
    const script = resolve(__dirname, '../crawler/traller-list'); 
    console.log(script);
    
    const child = childProcess.fork(script, []);
    let invoked = false;

    child.on('error', err => {
        if (invoked) return;
        invoked = true;
        console.log(err);
    })

    child.on('exit', code => {
        if (invoked) return;
        invoked = true;
        let err = code === 0 ? null : new Error('exit code ' + code);
        console.log(err);
    })

    child.on('message', data => {
        let result = data.result;
        console.log(result);
    })

})()